<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车测试页面</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ccc; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        button { margin: 5px; padding: 10px; }
    </style>
</head>
<body>
    <h1>购物车功能测试</h1>
    
    <div class="test-section">
        <h3>1. 模块加载测试</h3>
        <div id="moduleStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 用户登录测试</h3>
        <button onclick="testLogin()">测试登录</button>
        <div id="loginStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 购物车操作测试</h3>
        <button onclick="testAddToCart()">添加商品到购物车</button>
        <button onclick="testGetCart()">获取购物车</button>
        <button onclick="testLogout()">登出</button>
        <div id="cartStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 控制台日志</h3>
        <div id="consoleLog"></div>
    </div>

    <!-- 引入必要的脚本 -->
    <script src="js/api.js"></script>
    <script src="js/auth.js"></script>
    <script src="js/cart.js"></script>
    
    <script>
        // 重写console.log来显示在页面上
        const originalLog = console.log;
        const originalWarn = console.warn;
        const originalError = console.error;
        
        function addToConsole(message, type = 'info') {
            const logDiv = document.getElementById('consoleLog');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `<div class="${type}">[${timestamp}] ${message}</div>`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        console.log = function(...args) {
            originalLog.apply(console, args);
            addToConsole(args.join(' '), 'info');
        };
        
        console.warn = function(...args) {
            originalWarn.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        console.error = function(...args) {
            originalError.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        // 测试模块加载
        function testModuleLoading() {
            const statusDiv = document.getElementById('moduleStatus');
            let status = '';
            
            if (window.api) {
                status += '<div class="success">✓ API模块已加载</div>';
            } else {
                status += '<div class="error">✗ API模块未加载</div>';
            }
            
            if (window.authManager) {
                status += '<div class="success">✓ AuthManager模块已加载</div>';
            } else {
                status += '<div class="error">✗ AuthManager模块未加载</div>';
            }
            
            if (window.cartManager) {
                status += '<div class="success">✓ CartManager模块已加载</div>';
            } else {
                status += '<div class="error">✗ CartManager模块未加载</div>';
            }
            
            statusDiv.innerHTML = status;
        }
        
        // 测试登录
        async function testLogin() {
            const statusDiv = document.getElementById('loginStatus');
            try {
                const result = await window.api.register({
                    username: 'testuser3',
                    password: 'testpass',
                    email: 'test3@example.com',
                    phone: '1234567890',
                    userType: 'Customer'
                });
                
                if (result.success) {
                    statusDiv.innerHTML = '<div class="success">✓ 注册/登录成功</div>';
                    console.log('登录成功:', result.data);
                } else {
                    // 如果注册失败，尝试登录
                    const loginResult = await window.api.login('testuser3', 'testpass');
                    if (loginResult.success) {
                        statusDiv.innerHTML = '<div class="success">✓ 登录成功</div>';
                        console.log('登录成功:', loginResult.data);
                    } else {
                        statusDiv.innerHTML = '<div class="error">✗ 登录失败: ' + loginResult.error + '</div>';
                    }
                }
            } catch (error) {
                statusDiv.innerHTML = '<div class="error">✗ 登录错误: ' + error.message + '</div>';
                console.error('登录错误:', error);
            }
        }
        
        // 测试添加商品到购物车
        async function testAddToCart() {
            const statusDiv = document.getElementById('cartStatus');
            try {
                if (window.cartManager) {
                    await window.cartManager.addToCart('F001', '东北大米', 115.3, 2);
                    statusDiv.innerHTML = '<div class="success">✓ 商品已添加到购物车</div>';
                } else {
                    statusDiv.innerHTML = '<div class="error">✗ CartManager未加载</div>';
                }
            } catch (error) {
                statusDiv.innerHTML = '<div class="error">✗ 添加商品失败: ' + error.message + '</div>';
                console.error('添加商品失败:', error);
            }
        }
        
        // 测试获取购物车
        async function testGetCart() {
            const statusDiv = document.getElementById('cartStatus');
            try {
                if (window.cartManager) {
                    const cartData = window.cartManager.getCartData();
                    statusDiv.innerHTML = '<div class="info">购物车数据: ' + JSON.stringify(cartData, null, 2) + '</div>';
                } else {
                    statusDiv.innerHTML = '<div class="error">✗ CartManager未加载</div>';
                }
            } catch (error) {
                statusDiv.innerHTML = '<div class="error">✗ 获取购物车失败: ' + error.message + '</div>';
                console.error('获取购物车失败:', error);
            }
        }
        
        // 测试登出
        function testLogout() {
            const statusDiv = document.getElementById('cartStatus');
            try {
                if (window.authManager) {
                    window.authManager.logout();
                    statusDiv.innerHTML = '<div class="info">✓ 已登出</div>';
                } else {
                    statusDiv.innerHTML = '<div class="error">✗ AuthManager未加载</div>';
                }
            } catch (error) {
                statusDiv.innerHTML = '<div class="error">✗ 登出失败: ' + error.message + '</div>';
                console.error('登出失败:', error);
            }
        }
        
        // 页面加载完成后运行测试
        window.addEventListener('load', () => {
            console.log('页面加载完成，开始测试...');
            testModuleLoading();
            
            // 延迟测试，确保所有模块都已加载
            setTimeout(() => {
                testModuleLoading();
            }, 500);
        });
    </script>
</body>
</html>
